package com.android.project.framework.util;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.android.project.framework.FileIO;

public class AssociationIO {
	public final static String file = ".association";
    public static  List<AssociationWords> listAss = new ArrayList<AssociationWords>();

    public static List<AssociationWords> load(FileIO files) {
    	ObjectInputStream in= null;
    	Log.d("parole ", ""  );
        try {
        	 in=new ObjectInputStream(files.readFile(file));
        	 Log.d("parole ", ""  );
             while(in != null){
               // highscores[i] = Integer.parseInt(in.readLine());
            	AssociationWords o = (AssociationWords)in.readObject();
            	Log.d("parole ", "" + o.getWord1()  );
            	listAss.add(o);
            	
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
        return listAss;
    }

    public static void save(FileIO files,Object o) {
    	Log.d("save ", "trovato"  );
    	ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(files.writeFileObject(file));
           
           out.writeObject(o);

        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }


    public static int find(FileIO files,AssociationWords o){
    	listAss = load(files);
    	int index = listAss.indexOf(o);
    	Log.d("find ", "trovato"  );
		if(index != -1){
			listAss.get(index).setPeso();
			Log.d("indice ", "trovato"  );
			return index;
		}
		return -1;
    	
    }
}
